<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <template v-if="showAllChecked">
      <el-checkbox v-model="allPageChecked" @change="handleChangeAllSize" :disabled="disableAllSize">所有页</el-checkbox>
    </template>
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :page-count="pageCount"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  data() {
    return {
      allPageChecked: false
    }
  },
  props: {
    total: {
      required: true,
      type: Number,
    },
    pageCount: {
      type: Number,
      default: 6,
    },
    page: {
      type: Number,
      default: 1,
    },
    limit: {
      type: Number,
      default: 15,
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 15, 20, 30, 50, 100, 200]
      },
    },
    // 'total, sizes, prev, pager, next, jumper'
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next',
    },
    background: {
      type: Boolean,
      default: true,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
    hidden: {
      type: Boolean,
      default: false,
    },
    showAllChecked: {
      type: Boolean,
      default: false
    },
    allChecked: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    allChecked(val) {
      this.allPageChecked = val
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      },
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      },
    },
    disableAllSize() {
      if(this.total === 0) {
        return true
      }
      return false
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleChangeAllSize() {
      this.$emit('all', this.allPageChecked);
    }
  },
}
</script>

<style lang="scss" scoped>
.pagination-container {
  /* position:absolute;
  bottom: 18px;
  z-index: 999;
  padding: 15px; */
  background-color: #fff;
  display: flex;
  align-items: center;

  ::v-deep .el-checkbox {
    margin-right: 15px !important;
  }
  ::v-deep .el-checkbox__label {
    font-size: 12px !important;
  }

  ::v-deep .el-pagination span:not([class*=suffix]), .el-pagination button{
    font-size: 12px !important;
  }

  ::v-deep .el-input__inner {
    font-size: 12px !important;
    height: 28px !important;
    line-height: 28px !important;
  }
  ::v-deep .el-pagination .el-select .el-input {
    width: 90px;
    margin: 0 5px;
}
::v-deep .el-pagination.is-background .btn-prev, .el-pagination.is-background .btn-next, .el-pagination.is-background .el-pager li {
    min-width: 28px !important;
}
}
.pagination-container.hidden {
  display: none;
}
/* 解决safari分页页码位置问题 */
::v-deep .el-input--medium .el-input__inner {
  line-height: 28px;
}

</style>
